package com.ls.kdlss.ui.mys;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.media.MediaPlayer;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.Environment;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.gm.common.utils.ToastUtil;
import com.gm.lib.utils.Compress;
import com.gm.lib.utils.UploadImgAdapter;
import com.gm.lib.views.titlebar.AbTitleBar;
import com.ls.kdlss.R;
import com.ls.kdlss.ui.home.DiZhiActivity;
import com.wx.android.common.util.ActivityUtil;

import java.io.File;
import java.io.IOException;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import de.hdodenhof.circleimageview.CircleImageView;
import okhttp3.OkHttpClient;

/**
 * Created by Administrator on 2016/11/11.
 */

public class MyInformationActivity extends AppCompatActivity {

    @Bind(R.id.title_bar)
    AbTitleBar titleBar;
    @Bind(R.id.iv_title)
    CircleImageView ivTitle;
    @Bind(R.id.iv_back)
    ImageView ivBack;
    @Bind(R.id.rl_titles)
    RelativeLayout rlTitles;
    @Bind(R.id.tv_name)
    TextView tvName;
    @Bind(R.id.iv_back2)
    ImageView ivBack2;
    @Bind(R.id.rl_nickname)
    RelativeLayout rlNickname;
    @Bind(R.id.tv_sex)
    TextView tvSex;
    @Bind(R.id.iv_back3)
    ImageView ivBack3;
    @Bind(R.id.rl_sex)
    RelativeLayout rlSex;
    @Bind(R.id.tv_region)
    TextView tvRegion;
    @Bind(R.id.iv_back4)
    ImageView ivBack4;
    @Bind(R.id.rl_diqu)
    RelativeLayout rlDiqu;
    @Bind(R.id.tv_yuyinjieshao)
    ImageView tvYuyinjieshao;
    @Bind(R.id.iv_back5)
    ImageView ivBack5;
    @Bind(R.id.rl_yuyin)
    RelativeLayout rlYuyin;
    @Bind(R.id.iv_back6)
    ImageView ivBack6;
    @Bind(R.id.rl_wenzi)
    RelativeLayout rlWenzi;
    @Bind(R.id.iv_back8)
    ImageView ivBack8;
    @Bind(R.id.rl_erweima)
    RelativeLayout rlErweima;
    @Bind(R.id.tv_yaoqing)
    TextView tvYaoqing;
    @Bind(R.id.iv_back9)
    ImageView ivBack9;
    @Bind(R.id.rl_yaoqingma)
    RelativeLayout rlYaoqingma;
    @Bind(R.id.iv_back11)
    ImageView ivBack11;
    @Bind(R.id.rl_renzheng)
    RelativeLayout rlRenzheng;
    @Bind(R.id.iv_back10)
    ImageView ivBack10;
    @Bind(R.id.rl_zige)
    RelativeLayout rlZige;
    @Bind(R.id.iv_back12)
    ImageView ivBack12;
    @Bind(R.id.rl_xiugaimima)
    RelativeLayout rlXiugaimima;
    int isonclick = 0;
    CountDownTimer countDownTimer;
    private static final String LOG_TAG = "AudioRecordTest";
    //语音文件保存路径
    private String FileName = null;

    //语音操作对象
    private MediaPlayer mPlayer = null;
    private MediaRecorder mRecorder = null;
    /**
     * Called when the activity is first created.
     */

    private UploadImgAdapter uploadImgAdapter;

    public static void launch(Context context) {

        ActivityUtil.startActivity(context, MyInformationActivity.class);
    }


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_information);
        ButterKnife.bind(this);
        //设置sdcard的路径
        FileName = Environment.getExternalStorageDirectory().getAbsolutePath();
        FileName += "/pander.3gp";
    }

    @OnClick({R.id.title_bar, R.id.iv_title, R.id.rl_titles, R.id.tv_name, R.id.rl_nickname, R.id.tv_sex, R.id.rl_sex, R.id.tv_region, R.id.rl_diqu, R.id.tv_yuyinjieshao, R.id.rl_yuyin, R.id.rl_wenzi, R.id.rl_erweima, R.id.tv_yaoqing, R.id.rl_yaoqingma, R.id.rl_renzheng, R.id.rl_zige, R.id.rl_xiugaimima})
    public void onClick(View view) {
        Dialog alertDialog = new Dialog(MyInformationActivity.this);
        Window window;
        switch (view.getId()) {

            case R.id.title_bar:

                break;
            case R.id.iv_title:
                break;
            case R.id.rl_titles:

                if (uploadImgAdapter == null) {
                    initUploadImgAdapter();
                }
                uploadImgAdapter.withMode(UploadImgAdapter.MODE_IMG).showDialog();
                break;
            case R.id.tv_name:
                break;
            case R.id.rl_nickname:


                alertDialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
                window = alertDialog.getWindow();
                alertDialog.show();
                window.setContentView(R.layout.dialog_nickname);

                break;
            case R.id.tv_sex:
                break;
            case R.id.rl_sex:


                alertDialog = new Dialog(MyInformationActivity.this);
                alertDialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
                window = alertDialog.getWindow();
                alertDialog.show();
                window.setContentView(R.layout.dialog_sex);

                break;
            case R.id.tv_region:
                break;
            case R.id.rl_diqu:

                DiZhiActivity.launch(MyInformationActivity.this);

                break;
            case R.id.tv_yuyinjieshao:
                break;
            case R.id.rl_yuyin:


                /**语音**/
                alertDialog = new Dialog(MyInformationActivity.this);
                alertDialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
                window = alertDialog.getWindow();
                alertDialog.show();
                window.setContentView(R.layout.dialog_luyin);
                ImageView im_start = (ImageView) window.findViewById(R.id.im_start);
                TextView tv_Again = (TextView) window.findViewById(R.id.tv_Again);
                final TextView tv_jishi = (TextView) window.findViewById(R.id.tv_jishi);
                im_start.setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View v) {

                        if (isonclick==0){
                            isonclick++;
                            ToastUtil.show("进入了方法点击图片");
                            daoJishi(tv_jishi);
                            startLuyin();
                        }else {
                            isonclick=0;
                            countDownTimer.cancel();
                            stopLuyin();
                        }


                    }
                });




                //录音
                tv_Again.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (isonclick==0){
                            bofangLuyin();
                        }
                    }
                });
                break;
            case R.id.rl_wenzi:
                break;
            case R.id.rl_erweima:
                break;
            case R.id.tv_yaoqing:
                break;
            case R.id.rl_yaoqingma:
                break;
            case R.id.rl_renzheng:
                break;
            case R.id.rl_zige:
                break;
            case R.id.rl_xiugaimima:
                break;
        }
    }


    private void initUploadImgAdapter() {
        Log.e("TAG", "进入了方法");
        uploadImgAdapter = new UploadImgAdapter(MyInformationActivity.this, new UploadImgAdapter.Action2() {
            @Override
            public void call(File file, int mode) {
                ivTitle.setImageBitmap(BitmapFactory.decodeFile(file.getAbsolutePath()));
                File file1 = Compress.compressImage(file.getPath(), MyInformationActivity.this);

            }
        });
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (uploadImgAdapter != null) {
            uploadImgAdapter.onActivityResult(requestCode, resultCode, data);
        }

    }

    /**
     * 开始录音
     **/
    public void startLuyin() {

        mRecorder = new MediaRecorder();
        mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
        mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
        mRecorder.setOutputFile(FileName);
        mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
        try {
            mRecorder.prepare();
        } catch (IOException e) {
            Log.e(LOG_TAG, "prepare() failed");
        }
        mRecorder.start();

    }

    /**
     * 停止录音
     **/

    public void stopLuyin() {
        // TODO Auto-generated method stub


        mRecorder.stop();
        mRecorder.release();
        mRecorder = null;
    }

    /**
     * 播放录音
     **/
    public void bofangLuyin() {
        // TODO Auto-generated method stub
        mPlayer = new MediaPlayer();
        try {
            mPlayer.setDataSource(FileName);
            mPlayer.prepare();
            mPlayer.start();
        } catch (IOException e) {
            Log.e(LOG_TAG, "播放失败");
        }

    }

    /**
     * 停止播放
     **/
    public void stopBofang() {
        // TODO Auto-generated method stub
        mPlayer.release();
        mPlayer = null;
    }


    /**
     * 倒计时
     **/
    public void daoJishi(final TextView tv_jishi) {
        ToastUtil.show("进入了方法倒计时");
        countDownTimer=  new CountDownTimer(1800000, 1) {
            int haomiao = 0;
            int miao = 0;
            int fen = 0;
            int xiaoshi = 0;
            String shijian;

            public void onTick(long millisUntilFinished) {
                haomiao++;
                if (haomiao == 60) {
                    haomiao = 0;
                    miao++;
                    if (miao == 60) {
                        miao = 0;
                        fen++;
                    }
                }
                shijian = xiaoshi + ":" + fen + ":" + miao + ":" + haomiao;
                tv_jishi.setText(shijian);

            }

            @Override
            public void onFinish() {

            }
        }.start() ;

    }

}
